package org.jsserv;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.View;

/**
 * Enables rendering a {@link View} to a String.
 * @author shelmberger
 */
public class DOMResponseWrapper extends HttpServletResponseWrapper
{
    protected static Logger log = Logger.getLogger(DOMResponseWrapper.class);
    private StringWriter sw=new StringWriter();
    private PrintWriter out=new PrintWriter(sw);
    
    public DOMResponseWrapper(HttpServletResponse response)
    {
        super(response);
    }

    @Override
    public PrintWriter getWriter() throws IOException
    {
        return out;
    }        
    
    @Override
    public ServletOutputStream getOutputStream() throws IOException
    {
        ServletOutputStream out=super.getOutputStream();
        log.debug("ServletOutputStream "+out);
        return out;
        
    }
    
    public String getOutput()
    {
        return sw.toString();
    }
    @Override
    public void flushBuffer() throws IOException
    {
        out.flush();
    }
}
